package com.system.connect.springboot_flowers.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.system.connect.springboot_flowers.dto.FavoritesDto;
import com.system.connect.springboot_flowers.entity.Favorites;
import com.system.connect.springboot_flowers.entity.Originalposts;
import com.system.connect.springboot_flowers.entity.Users;
import com.system.connect.springboot_flowers.service.IFavoritesService;
import com.system.connect.springboot_flowers.service.IOriginalpostsService;
import com.system.connect.springboot_flowers.service.IUsersService;
import com.system.connect.springboot_flowers.utils.R;
import com.system.connect.springboot_flowers.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * <p>
 * 收藏表 前端控制器
 * </p>
 *
 * @author yang
 * @since 2025-01-13
 */
@RestController
@RequestMapping("/api/favorites")
public class FavoritesController {

    @Autowired
    private IFavoritesService favoritesService;

    @Autowired
    private IOriginalpostsService postsService;

    @Autowired
    private IUsersService usersService;

    @PostMapping("/save")
    public R save(Integer id) {
        System.out.println("帖子id"+id);
        QueryWrapper<Favorites> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Target_id",id);
        Integer userid = Integer.valueOf(TokenUtils.getUser());
        queryWrapper.eq("user_id",userid);
        if (favoritesService.count(queryWrapper)>0){
            System.out.println("帖子已存在");
            return R.error("帖子已收藏");
        }
        Favorites favorites = new Favorites();
        favorites.setUserId(userid);
        favorites.setCategory("original");
        favorites.setTargetId(id);
        favoritesService.save(favorites);

        return R.success("帖子收藏成功");
    }

    /**
     * 帖子收藏列表
     */
    @GetMapping("/list")
    public R list() {
        Integer userid = Integer.valueOf(TokenUtils.getUser());
        QueryWrapper<Favorites> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userid);

        FavoritesDto favoritesDto = new FavoritesDto();
        ArrayList<FavoritesDto> favoritesDtos = new ArrayList<>();

        for (Favorites favorites : favoritesService.list(queryWrapper)) {
            Originalposts originalposts = postsService.getById(favorites.getTargetId());
            Users users =usersService.getById(originalposts.getUserId());
            favoritesDto.setPostId(originalposts.getOriginalpostid());
            favoritesDto.setTitle(originalposts.getTiele());
            favoritesDto.setContent(originalposts.getContent());
            favoritesDto.setAuthorId(users.getUserId());
            favoritesDto.setAuthorAvatar(users.getAvatarUrl());
            favoritesDto.setCreatedAt(LocalDateTime.parse(originalposts.getCreatedAt().toString()));
            favoritesDtos.add(favoritesDto);
        }

        return R.success(favoritesDtos);
    }
}
